<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('备份还原')" />
</head>
<body class="gray-bg">
	<div class="container-div">
		<div class="row">
			<div class="btn-group-sm" id="toolbar" role="group">
				<button class="btn btn-success dim" type="button" onclick="backup()" disabled="true">
					<i class="fa fa-database"></i> 当前库名：[[${dsName}]]
				</button>
				<button class="btn btn-success dim" type="button" onclick="backup()">
					<i class="fa fa-save"></i> 备份
				</button>
				<button class="btn btn-info dim" type="button" onclick="logs(1)">
					<i class="fa fa-info-circle"></i> 备份日志
				</button>

				<button class="btn btn-primary dim" type="button" onclick="$('#formFile').click();" style="margin-left: 100px;">
					<i class="fa fa-upload"></i> 本地数据还原
				</button>
				<form style="display: none" id="uploadForm">
					<label for="formFile"></label><input th:type="file" id="formFile" onchange="restore(null, 1)" name="file"/>
				</form>
				<button class="btn btn-info dim" type="button" onclick="logs(2)">
					<i class="fa fa-info-circle"></i> 还原日志
				</button>

			</div>
	        
	        <div class="col-sm-12 select-table table-striped">
			    <table id="bootstrap-table"></table>
			</div>
		</div>
	</div>
	<th:block th:include="include :: footer" />
	<script th:inline="javascript">
		var prefix = ctx + "system/data";
		let dbName = [[${dsName}]]

		function logs(type){
			let url = prefix + '/logs?type=' + type
			let titleStr = type === 1 ? '最近的备份日志' : '最近的还原日志';
			$.ajax({
				type: "POST",
				url: url,
				success: (d) => {
					if (d.code !== 0){
						layer.msg(d.msg)
					}else {
						let ct = '',
							head = '<tr><th>文件</th><th>发生时间</th><th>日志内容</th></tr>';
						d.data.forEach((item, idx) => {
							ct += '<tr><td>'+item.optFile+'</td><td>'+item.time+'</td><td><textarea style="height: 69px; width: 430px;">'+item.log+'</textarea></td></tr>'
						})
						ct = head + ct;
						layer.open({
							title: titleStr,
							content: '<table class="table">' + ct + '</table>'
						});
					}
				}
			})
		}

		function backup(){
			layer.prompt({
				formType: 0,
				value: '不填',
				title: '备份后通知邮箱，不填则不推送'
			}, function(value, index, elem){
				layer.close(index);

				let url = prefix + '/opt?opt=1&mailTo=' + value
				$.ajax({
					type: "POST",
					url: url,
					processData: false,
					contentType: false,
					success: (d) => {
						console.info(d)
						if (d.code === 0){
							$.table.search();
						}
						layer.msg(d.msg)
					}
				})
			});
		}

		function del(fileName){
			layer.confirm('您确定要删除该文件吗?',{btn: ['确定', '取消'],title:"提示"}, function(){
				let url = prefix + '/del?fileId=' + fileName
				$.ajax({
					type: "POST",
					url: url,
					success: (d) => {
						layer.alert(d.msg, () => {
							$.table.search();
						})
					}
				})
			});
		}

		function restore(fileName, file){
			let url = prefix + '/opt/?opt=2'
			if (fileName){
				url += "&fileId=" + fileName;
			}
			if (file){
				$.ajax({
					type: "POST",
					url: url,
					data: new FormData($('#uploadForm')[0]),
					processData: false,
					contentType: false,
					success: callback
				})
			}else{
				$.ajax({
					type: "POST",
					url: url,
					success: callback
				})
			}

			function callback(d){
				layer.alert(d.msg)
			}
		}

		$(function() {
		    var options = {
		        url: prefix + "/files?dbName=" + dbName,
		        updateUrl: prefix + "/opt",
		        modalName: "已备份文件",
		        columns: [
				{
					field: 'dbName',
					title: '库名'
				},
		        {
		            field: 'name',
		            title: '文件名'
		        },
		        {
		            field: 'size',
		            title: '文件大小(kb)'
		        },
		        {
		            field: 'lastModified',
		            title: '最后修改时间'
		        },
		        {
		            field: 'path',
		            title: '文件路径'
		        },
		        {
		            title: '操作',
		            align: 'center',
		            formatter: function(value, row, index) {
		                var actions = [];
		                actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="restore(\'' + row.name + '\')"><i class="fa fa-paste"></i> 还原</a> ');
		                actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="del(\'' + row.name + '\')"><i class="fa fa-paste"></i> 删除</a> ');
		                return actions.join('');
		            }
		        }]
		    };
		    $.table.init(options);
		});
	</script>
</body>
</html>